﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;

namespace ICarPool.Common
{
    /// <summary>
    /// Ip地址转换器，使用有道提供的接口
    /// </summary>
    public static class IpTransformer
    {
        static string strYoDao = "http://www.youdao.com/smartresult-xml/search.s?type=ip&q=";

        #region  读取http://www.yodao.comIP地址接口
       /// <summary>
        /// 读取http://www.yodao.com接口IP地址;通过ip地址,获取省市信息
       /// </summary>
       /// <param name="strIP"></param>
       /// <returns></returns>
        public static IList<string> GetIpAddress(string strIP)
        {
            List<string> result = new List<string>();

            //获取youdao返回的xml格式文件内容
            using (XmlReader read = XmlReader.Create(strYoDao + strIP))
            {
                while (read.Read())
                {
                    switch (read.NodeType)
                    {
                        //取xml格式文件当中的文本内容
                        case XmlNodeType.Text:
                            //youdao返回的xml格式文件内容一个是IP，另一个是IP地址，如果不是IP那么就是IP地址
                            if (string.Format("{0}", read.Value).Trim() != strIP)
                            {
                                string ipAddress = string.Format("{0}", read.Value);
                                int provinceIndex = ipAddress.IndexOf('省');

                                //包含省，则获取省份
                                if (provinceIndex > 0)
                                {
                                    string strProvince = ipAddress.Substring(0, provinceIndex + 1);
                                    result.Add(strProvince);
                                }

                                int cityIndex = ipAddress.IndexOf('市');
                                //包含市，则获取市
                                if (cityIndex > 0)
                                {
                                    string strCity = string.Empty;
                                    if (provinceIndex > 0)
                                    {
                                       //普通市
                                        strCity = ipAddress.Substring(provinceIndex + 1, cityIndex - provinceIndex);
                                    }
                                    else
                                    {
                                        //直辖市
                                        strCity = ipAddress.Substring(0, cityIndex + 1);
                                    }
                                    result.Add(strCity);
                                }  
                            }
                            break;
                    }
                }
            }

            return result;
        }
        #endregion 

        /// <summary>
        /// 获取IP地址
        /// </summary>
        public static string UserIPAddress
        {
            get
            {
                string userIP;
                HttpRequest Request = HttpContext.Current.Request; // ForumContext.Current.Context.Request;
                // 如果使用代理，获取真实IP
                if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != string.Empty)
                    userIP = Request.ServerVariables["REMOTE_ADDR"];
                else
                    userIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
               
                if (string.IsNullOrEmpty(userIP))
                    userIP = Request.UserHostAddress;

                return userIP;
            }
        }
    }

    
}
